package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.AllotAreaSet;
import com.xmy.cultivate.entity.AllotTeacherWheel;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.StationNeeds;
import com.xmy.cultivate.mapper.AllotTeacherWheelMapper;
import com.xmy.cultivate.mapper.RecruitMapper;
import com.xmy.cultivate.mapper.StationNeedsMapper;
import com.xmy.cultivate.service.IAllotTeacherWheelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 校区分配教师的轮数 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2025-06-07
 */
@Service
public class AllotTeacherWheelServiceImpl extends ServiceImpl<AllotTeacherWheelMapper, AllotTeacherWheel> implements IAllotTeacherWheelService {

    @Autowired
    AllotTeacherWheelMapper allotTeacherWheelMapper;

    @Autowired
    StationNeedsMapper stationNeedsMapper;

    @Autowired
    RecruitMapper recruitMapper;

    /**
     * 获取应分配的校区id
     * @param yearPart
     * @param quarterNum
     * @param stationNeedsId
     * @param areaId
     * @return
     */
    @Override
    public Long getAllotSchoolId(Integer yearPart, Integer quarterNum, Long recruitId, Long stationNeedsId, Long areaId, Long inviterId) {

        Long allotSchoolId = null;

        StationNeeds stationNeeds = stationNeedsMapper.selectById(stationNeedsId);
        Integer type = stationNeeds.getType();
        //部门岗，直接返回总部id
        if (type.equals(2)) {
            //部门岗
            //return 1600466723094175746L;//部门岗，直接返回总部id
            areaId = 0L;//部门不分区域，因为这里设置为0
            allotSchoolId = allotTeacherWheelMapper.getSchoolNeedsCount(yearPart,quarterNum,null,stationNeeds.getStationId(),stationNeeds.getSubjectsId(),areaId,2);
            return allotSchoolId;
        }else {
            //校区岗,校区招优先分给校区
            //判断是校区招，还是部门招
            Integer personType = recruitMapper.getPersonType(recruitId);
            if (personType != null){
                if (personType.equals(1)) {
                    Staff inviterInfo = allotTeacherWheelMapper.getStaffSchoolInfo(yearPart,quarterNum,inviterId);
                    if (Objects.nonNull(inviterInfo)) {
                        if (inviterInfo.getSchoolId()!=null){
                            //查看校区是否还有需求
                            Long schoolNeedsCount = allotTeacherWheelMapper.getSchoolNeedsCount(yearPart,quarterNum,inviterInfo.getSchoolId(),stationNeeds.getStationId(),stationNeeds.getSubjectsId(),areaId,1);
                            if (schoolNeedsCount>0){
                                allotSchoolId = inviterInfo.getSchoolId();
                            }
                        }
                    }
                }
            }
        }
        if (allotSchoolId != null){
            return allotSchoolId;
        }
        addAllotTeacherWheel(yearPart,quarterNum,stationNeedsId,areaId);//添加轮数
        AllotTeacherWheel allotTeacherWheel = allotTeacherWheelMapper.getOneForNeeds(yearPart,quarterNum,stationNeeds.getStationId(),stationNeeds.getSubjectsId(),areaId);//获取应分的校区
        if (Objects.nonNull(allotTeacherWheel)) {
            allotSchoolId = allotTeacherWheel.getSchoolId();
        }else {
            Long count = allotTeacherWheelMapper.getSchoolNeedsCount(yearPart,quarterNum,null,stationNeeds.getStationId(),stationNeeds.getSubjectsId(),areaId,1);
            if (count >0){
                allotTeacherWheelMapper.updateYetNum(stationNeeds.getStationId(),stationNeeds.getSubjectsId(),areaId);
                //重新获取
                allotTeacherWheel = allotTeacherWheelMapper.getOneForNeeds(yearPart,quarterNum,stationNeeds.getStationId(),stationNeeds.getSubjectsId(),areaId);//获取应分的校区
                if (Objects.nonNull(allotTeacherWheel)) {
                    allotSchoolId = allotTeacherWheel.getSchoolId();
                }
            }
        }
        return allotSchoolId;
    }

    public boolean addAllotTeacherWheel(Integer yearPart,Integer quarterNum,Long stationNeedsId,Long areaId) {
        StationNeeds stationNeeds = stationNeedsMapper.selectById(stationNeedsId);
        //********************************添加没有添加过的校区开始*****************************************************//
        List<AllotAreaSet> allotAreaSetList = this.getUnAddList(yearPart,quarterNum,stationNeeds.getStationId(),stationNeeds.getSubjectsId(),areaId);
        for (AllotAreaSet allotAreaSet : allotAreaSetList) {
            AllotTeacherWheel allotTeacherWheel = new AllotTeacherWheel();
            allotTeacherWheel.setYearPart(yearPart);
            allotTeacherWheel.setQuarterNum(quarterNum);
            allotTeacherWheel.setAreaId(areaId);
            allotTeacherWheel.setStationNeedsId(stationNeeds.getId());
            allotTeacherWheel.setStationId(stationNeeds.getStationId());
            allotTeacherWheel.setSubjectsId(stationNeeds.getSubjectsId());
            allotTeacherWheel.setSchoolId(allotAreaSet.getSchoolId());
            allotTeacherWheel.setLowestNum(allotAreaSet.getScaleNum());
            allotTeacherWheel.setYetNum(0);
            allotTeacherWheel.setRoundNum(1);
            allotTeacherWheelMapper.insert(allotTeacherWheel);
        }
        //********************************添加没有添加过的校区结束*****************************************************//
        return true;
    }


    @Override
    public List<AllotAreaSet> getUnAddList(@Param("yearPart") Integer yearPart,
                                           @Param("quarterNum") Integer quarterNum,
                                           @Param("stationId") Long stationId,
                                           @Param("subjectsId") Long subjectsId,
                                           @Param("areaId") Long areaId){
        return allotTeacherWheelMapper.getUnAddList(yearPart,quarterNum,stationId,subjectsId,areaId);
    }

    @Override
    public Long getSchoolNeedsCount(@Param("yearPart") Integer yearPart,
                                       @Param("quarterNum") Integer quarterNum,
                                       @Param("schoolId") Long schoolId,
                                       @Param("stationId") Long stationId,
                                       @Param("subjectsId") Long subjectsId,
                                       @Param("areaId") Long areaId){
        return allotTeacherWheelMapper.getSchoolNeedsCount(yearPart,quarterNum,schoolId,stationId,subjectsId,areaId,1);
    }

    @Override
    public Staff getStaffSchoolInfo(Integer yearPart,
                                    Integer quarterNum,
                                    Long teacherId
    ){
        return allotTeacherWheelMapper.getStaffSchoolInfo(yearPart,quarterNum,teacherId);
    }
}
